/**
 * 给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点.
 * 
 * 思路:双指针法:定义一个虚拟头结点,fast指针和slow指针,fast指针先
 * 移动n位,slow指针随后移动,当fast.next = null时,删除slow指针所
 * 指向的结点.
 */
class ListNode {
  constructor(val, next) {
    this.val = val;
    this.next = next;
  }
}

function removeNthFromEnd(head, n) {
  let ret = new ListNode(0, head);
  let fast = ret;
  let slow = ret;
  while (n--) {
    fast = fast.next;
  }
  while (fast.next !== null) {
    fast = fast.next;
    slow = slow.next;
  }
  slow.next = slow.next.next;
  return ret.next;
}